/* Define inherits functions from parent class */
Function.prototype.inherits = function(superclass, implementation) {
	this.prototype = implementation;
	this.prototype.parent = function() {
		var fn = arguments.callee.caller;
		if (!fn._super)
			return;
		return fn._super.apply(this, arguments);
	};

	this.prototype.constructor = this;

	for (var k in implementation) {
		if (implementation[k] instanceof Function && superclass.prototype[k] instanceof Function) {
			implementation[k]._super = superclass.prototype[k];
		}
	}
	this.prototype.__proto__ = superclass.prototype;
};

/* Common function */
var $id = function(id) {
	var node = document.getElementById(id);
	return node ? node : document.getElementsByTagName(id)[0];
}
function createDiv() {
	return document.createElement("div");
}

function createNode(type) {
	return document.createElement(type);
}

function isDefined(obj) {
	return ( typeof (obj) != "undefined" && obj != null);
}

/* Implement Element object */
var _Ep = Element.prototype;

_Ep.listen = function(eventType, callback, context) {
    var cb = function(event) {
        if (context) {
            context[callback](event);
        } else {
            callback(event);
        }
    }
    this.addEventListener(eventType, cb, true);
}

_Ep.click = function(callback) {
	var cb = callback();
    this.addEventListener("click", cb, true);
}

_Ep.hasClass = function(className) {
	return this.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
}

_Ep.addClass = function(className) {
	return !this.hasClass(className) ? this.className += " " + className : this.className;
}

_Ep.removeClass = function(className) {
	if (this.hasClass(className)) {
		var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
		this.className = this.className.replace(reg, ' ');
	}
}

_Ep.setId = function(name) {
	this.setAttribute("id", name);
}

_Ep.setText = function(text) {
	this.innerText = text;
}
